package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;


public class RecyclerViewDemoActivity extends AppCompatActivity {

    private RecyclerView recyclerView;//声明RecyclerView
    private List<Contact> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_demo);
        recyclerView = findViewById(R.id.recycler_view_demo);


        //1.
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);

        //2.
        ContactAdapter adapter = new ContactAdapter(this);
        recyclerView.setAdapter(adapter);

        //3.
        List<Contact> contacts = initData();
        adapter.setData(list);
    }

    private List<Contact> initData(){

        for (int i = 0; i < 20; i++){
            Contact contact = new Contact();
            contact.setName("jack");
            list.add(contact);
        }
        return list;
    }
}